import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
// @ts-ignore
import eslintPlugin from "vite-plugin-eslint";
import type { UserConfigExport, ConfigEnv } from "vite";

export default ({ mode }: ConfigEnv): UserConfigExport => {
  const env = loadEnv(mode, process.cwd(), "VITE_");
  const pathSrc = path.resolve(__dirname, "src");
  return {
    base: env.VITE_PUBLIC_PATH,
    server: {
      host: "0.0.0.0",
      port: Number(env.VITE_DEFAULT_PORT),
      proxy: {
        "/api": {
          target: env.VITE_PROXY_API,
          changeOrigin: true,
        },
      },
    },
    build: {
      outDir: "/dist/",
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue"],
        dts: path.resolve(pathSrc, "auto-imports.d.ts"),
        resolvers: [ElementPlusResolver()],
        eslintrc: {
          enabled: true,
        },
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dts: path.resolve(pathSrc, "components.d.ts"),
      }),
      eslintPlugin({
        include: ["src/**/*.ts", "src/**/*.vue", "src/*.ts", "src/*.vue"],
      }),
    ],
    resolve: {
      alias: {
        "@": pathSrc,
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
          additionalData: "@use '@/styles/var.scss' as *;", // 引入var文件
        },
      },
    },
  };
};
